//
//  EmotionGridView.m
//  Weibo
//
//  Created by Anasue on 9/25/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "EmotionGridView.h"
#import "Emotion.h"
#import "EmotionView.h"
#import "EmotionPopView.h"
#import "EmotionTool.h"


@interface EmotionGridView ()
@property (nonatomic, weak) UIButton *deleteBtn;
@property (nonatomic, strong) NSMutableArray *emotionViews;
@property (nonatomic, strong) EmotionPopView *popView;

@end

@implementation EmotionGridView

#pragma mark - 懒加载
- (NSMutableArray *)emotionViews
{
    if (_emotionViews == nil) {
        _emotionViews = [NSMutableArray array];
    }
    return _emotionViews;
}

- (EmotionPopView *)popView
{
    if (_popView == nil) {
        _popView = [EmotionPopView popView];
    }
    return _popView;
}

#pragma mark - 初始化
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //添加删除按钮
        UIButton *deleteBtn = [[UIButton alloc]init];
        [deleteBtn setImage:[UIImage imageWithName:@"compose_emotion_delete"] forState:UIControlStateNormal];
        [deleteBtn setImage:[UIImage imageWithName:@"compose_emotion_delete_highlighted"] forState:UIControlStateHighlighted];
        [deleteBtn addTarget:self action:@selector(deleteClickEmotion) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:deleteBtn];
        self.deleteBtn = deleteBtn;
        
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]init];
        [longPressGesture addTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:longPressGesture];
    }
    return self;
}

#pragma mark - longPressGesture
- (EmotionView *)emotionViewWithPoint:(CGPoint)point
{
    __block EmotionView *foundEmotionView = nil;
    [self.emotionViews enumerateObjectsUsingBlock:^(EmotionView *emotionView, NSUInteger idx, BOOL * _Nonnull stop) {
        if (CGRectContainsPoint(emotionView.frame, point)) {
            foundEmotionView = emotionView;
            *stop = YES;
        }
    }];
    return foundEmotionView;
}

- (void)longPress:(UILongPressGestureRecognizer *)longPressGesture
{
    CGPoint point = [longPressGesture locationInView:longPressGesture.view];
    EmotionView *emotionView = [self emotionViewWithPoint:point];
    if (longPressGesture.state == UIGestureRecognizerStateEnded) {
        [self.popView dismiss];
        [self selectEmotion:emotionView.emotion];
    }else{
        [self.popView showPopView:emotionView];
    }
    
}

- (void)setEmotions:(NSArray *)emotions
{
    _emotions = emotions;
    NSInteger count = emotions.count;
    NSInteger currentGridViewCount = self.emotionViews.count;
    for (int i = 0; i < count; i++) {
        EmotionView *emotionView = nil;
        if (i >= currentGridViewCount) {
            emotionView = [[EmotionView alloc]init];
            [emotionView addTarget:self action:@selector(emotionClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:emotionView];
            [self.emotionViews addObject:emotionView];
        }else{
            emotionView = self.emotionViews[i];
        }
        emotionView.emotion = emotions[i];
        emotionView.hidden = NO;
    }
    for (NSInteger i = count; i < currentGridViewCount; i ++) {
        UIButton *emotionView = self.emotionViews[i];
        emotionView.hidden = YES;
    }
}

- (void)emotionClick:(EmotionView *)emotionView
{
    [self.popView showPopView:emotionView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.popView dismiss];
    });
    [self selectEmotion:emotionView.emotion];
}

- (void)selectEmotion:(Emotion *)emotion
{
    if (emotion == nil) return;
    [EmotionTool addRecentEmotion:emotion];
    [[NSNotificationCenter defaultCenter] postNotificationName:EmotionDidSelectNotification object:nil userInfo:@{SelectedEmotion:emotion}];
}

- (void)deleteClickEmotion
{
    [[NSNotificationCenter defaultCenter] postNotificationName:EmotionDidDeleteNotification object:nil userInfo:nil];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    int maggin = 10;
    NSInteger count = self.emotionViews.count;
    CGFloat btnViewW = (self.width - 2 * maggin) / EmotionMaxColsPerPage;
    CGFloat btnViewH = (self.height - maggin) / EmotionMaxRowsPerPage;
    for (int i = 0; i < count; i++) {
        UIButton *btnView = self.emotionViews[i];
        btnView.width = btnViewW;
        btnView.height = btnViewH;
        btnView.x = maggin + (i % EmotionMaxColsPerPage) * btnViewW;
        btnView.y = maggin + (i / EmotionMaxColsPerPage) * btnViewH;
    }
    //删除键frame
    self.deleteBtn.width = btnViewW;
    self.deleteBtn.height = btnViewH;
    self.deleteBtn.x = self.width - maggin - self.deleteBtn.width;
    self.deleteBtn.y = self.height - self.deleteBtn.height;
}

@end
